package com.mid;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lxk on 2019/12/1.
 */
public class Solution781 {

    public int numRabbits(int[] answers) {
        if (answers == null || answers.length == 0) {
            return 0;
        }
        Map<Integer, Integer> countMap = new HashMap<>();
        for (Integer answer : answers) {
            if (countMap.keySet().contains(answer)) {
                //包含则统计加一
                countMap.put(answer, countMap.get(answer) + 1);
            } else {
                //不包含则初始为一
                countMap.put(answer, 1);
            }
        }
        int count = 0;
        for (Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
            if (entry.getKey() + 1 >= entry.getValue()) {
                //相同数超过参与统计数，表明可以采用相同数
                count += entry.getKey() + 1;
            } else {
                //相同数小于参与统计数，表明可以按照相同进行分割
                double res = (entry.getValue()) / (entry.getKey() + 1);
                count += Math.floor(res) * (entry.getKey() + 1);
                //判断是否整除，非整除数量需要增加
                if (Math.abs(Math.floor(res) * (entry.getKey() + 1) - entry.getValue()) >= 1) {
                    count += entry.getKey() + 1;
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] arr = {0, 0, 1, 1, 1};
        Solution781 solution781 = new Solution781();
        System.out.println(solution781.numRabbits(arr));
    }

}
